﻿Standard_List.render = function() {

    var Standard_List_cache = {};
    var Standard_List_href='';
    var Standard_List = {

        init: function() {
            var that = this;

            $.ajax({
                url: COMMONURL + 'api/Account/GetSendMeum',
                dataType: 'jsonp',
                jsonp: 'jsonpcallback',
                data: {
                    type: 1,
                    token: $.cookie('token')
                }
            }).done(function(data) {
                var menu = data.menu;
                var compiled = _.template($("#Standard_List_nav").html());
                var compiled_cont = _.template($("#Standard_List_cont").html());
                $("#fiHeader").html(compiled(menu));
                $("#myTabContent").html(compiled_cont(menu));

                that.firstaction();
                that.shownaction();

                //废标
                $('#delModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        itemid = button.data('itemid'),
                        bid_no = button.data('bidno');
                    $(this).attr({
                        'itemid': itemid,
                        'bid_no': bid_no
                    });
                });

                //废标备注
                $("#delModelSubmit").click(function() {
                    var itemid = $('#delModel').attr('itemid'),
                        bid_no = $('#delModel').attr('bid_no'),
                        reason = $.trim($("#reason").text()),
                        remark = $.trim($("#message-text").text());
                    $.ajax({
                        url: COMMONURL + "api/bidbase/ScrapBid?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            bid_id: itemid,
                            bid_no: bid_no,
                            close_reason: reason,
                            reason_info: remark
                        }
                    }).done(function(redata) {
                    	if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;
            }
                        if (redata.recode == '0000') {
                            alert('操作成功');
                            $('#delModel').modal('hide');
                            window.location.reload();
                        }
                    });

                });

                //选标
                $('#selModel').on('show.bs.modal', function(event) {
                    var button = $(event.relatedTarget),
                        itemid = button.data('itemid');
                    $.ajax({
                        url: COMMONURL + "api/BidPrice/GetPriceList?token=" + $.cookie('token'),
                        type: "get",
                        data: {
                            bid_id: itemid
                        },
                        dataType: "json"
                    }).done(function(redata) {
                        if (redata.recode == '0000') {
                            var data = redata.mod;
                            var compiled = _.template($("#selTpl").html());
                            $("#selModelCon").html(compiled(data));
                        }
                    });
                });

                //选定
                $('#selModelCon').on('click', '[data-name="selBid"]', function() {
                    var bid_wl_id = $(this).attr('data-itemid');
                    var bid_id = $(this).attr('data-mybid');
                    $.ajax({
                        url: COMMONURL + "api/BidPrice/ChoiceBidPrice?token=" + $.cookie('token'),
                        type: "post",
                        data: {
                            "bid_wl_id": bid_wl_id,
                            "bid_id": bid_id
                        },
                        dataType: "json"
                    }).done(function(redata) {
                    	if(redata.recode=='1003'){
                window.location.href = '/nlogin.html';
                return;
            }
                        if (redata.recode == '0000') {
                            alert(redata.resmessage);
                            window.location.reload();
                        }
                    });
                });
            });
        },
        shownaction: function() {
            $('a[data-toggle="tab"]').on('shown.bs.tab', function(e) {
                // alert(e.target.href) // newly activated tab
                var idname = $(e.target).attr('href').replace('#', '');
                var index = $('a[data-toggle="tab"]').index($(e.target))
                
                 $.cookie('Standard_List_index', idname);
                $.cookie('Standard_List_point', index);

                if (Standard_List_cache[idname]) {
                    $('#' + idname).html(Standard_List_cache[idname]);
                    $("#addItem").hide();
                } else {
                    $.ajax({
                        url: idname + '.html'
                    }).done(function(data) {
                        Standard_List_cache[idname] = data;
                        $('#' + idname).html(data);

                        $("#addItem").hide();
                    })
                }
            })
        },
        firstaction: function() {
            
             var firstName=$('a[data-toggle="tab"]').eq(0).attr('href').replace('#', '');
            //var point = location.hash.replace('#','').split("&")[1];
            if($.cookie('Standard_List_point')!=0 && $.cookie('Standard_List_point')){
                 if(!!$.cookie('Standard_List_index')){
                firstName = $.cookie('Standard_List_index');
                $('a[href="#'+firstName+'"]').tab('show');
                return;
                }
            }
          else{
            if($.cookie('Standard_List_index') && firstName != $.cookie('Standard_List_index')){
                firstName=$.cookie('Standard_List_index');
                $('a[href="#'+firstName+'"]').tab('show');
              
            }else{
            
                if (Standard_List_cache[firstName]) {

                $('#' + firstName).html(Standard_List_cache[firstName]);
            } else {

                $.ajax({
                    url: firstName + '.html'
                }).done(function(data) {
                    Standard_List_cache[firstName] = data;
                    $('#' + firstName).html(data);


                })
            }
            }
            
        }
        }

   }
    Standard_List.init();

}
